package com.java.dao.impl;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.HibernateTransactionManager;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

import com.java.dao.DeptDao;
import com.training.pojo.chengshu.MotorRoom;
import com.training.pojo.chengshu.UDept;
import com.training.pojo.chengshu.UUser;

public class DeptDaoImpl extends HibernateDaoSupport implements DeptDao{

	private List<UDept> list1;
	private Integer count;
	//private Integer n;
	
	//部门列表
	public List<UDept> deptList(final int start) {
		final String hql = "from UDept where isdelete=0"; 
		Object o = this.getHibernateTemplate().execute(new HibernateCallback<Object>() {

			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query q = session.createQuery(hql);
				q.setFirstResult(start);
				q.setMaxResults(2);
				list1 = q.list();
				return list1;
			}
			
		});
		return list1;
	}
	
	//部门数量
	public Integer countDept() {
		final String hql ="from UDept where isdelete=0";
		Object o = this.getHibernateTemplate().execute(new HibernateCallback<Object>() {

			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query q = session.createQuery(hql);
				List<UDept> list = q.list();
				count = list.size();
				//System.out.println(count+"+++++++++++");
				return count;
			}
			
		});
		return count;
	}
	
	//部门员工数量
	public Integer countUUser(int deptid) {
		String hql ="from UUser where UDept.deptid = ?";
		List<UUser> list = this.getHibernateTemplate().find(hql,deptid);
		//System.out.println(list.size()+"dddddddd");
		return list.size();
	}
	//主管列表
	public List<UUser> userList() {
		String hql = "from UUser where utype = 1";
		List<UUser> list = this.getHibernateTemplate().find(hql);
		for(UUser u : list){
			//System.out.println(u.getUtype()+"+"+u.getUname());
		}
		return list;
	}
    //部门员工列表
	public List<UUser> empList(int deptid) {
		String hql = "from UUser where UDept.deptid = ?";
		List<UUser> list = this.getHibernateTemplate().find(hql, deptid);
		for(UUser u :list){
			//System.out.println(u.getUtype()+"++"+u.getUid());
		}
		return list;
	}
	//获取部门
	public UDept getDept(int deptid) {
		UDept d = this.getHibernateTemplate().get(UDept.class, deptid);
		System.out.println(d.getDeptname()+"+"+d.getDeptid());
		return d;
	}

    //删除部门
	public boolean deleDept(int deptid) {
		HibernateTemplate h =  this.getHibernateTemplate();
		UDept d = h.get(UDept.class, deptid);
		d.setIsdelete(1);
		h.update(d);
		return true;  
	}
    //添加部门
	public boolean addDept(UDept d) {
		Serializable si = this.getHibernateTemplate().save(d);
		//System.out.println(si);
		return true;
	}
    //修改部门 
	public UDept updateDept(UDept d) {
		HibernateTemplate  h=this.getHibernateTemplate();
		//System.out.println("------------"+d.getDeptname());
		h.update(d);
		UDept dd = h.get(UDept.class, d.getDeptid());
		//System.out.println("==========="+dd.getDeptname());
		return dd;
	}

	public void updateMotor(Integer deptid){
		//System.out.println("updateMotor11");
		
		String hql="from MotorRoom where UDept.deptid=?";
		HibernateTemplate h =  this.getHibernateTemplate();
		List<MotorRoom> list = h.find(hql,deptid);
		//System.out.println("updateMotor11"+list.size());
		
		for(MotorRoom m : list){
//			MotorRoom mm = new MotorRoom();
//			mm.setMotaddr(m.getMotaddr());
//			mm.setMotid(m.getMotid());
//			mm.setMotname(m.getMotname());
//			mm.setMotnote(m.getMotnote());
//			mm.setUUser(m.getUUser());
//			h.delete(m);
//			h.save(mm);
			m.setUDept(null);
			h.update(m);
			//System.out.println("updateMotor11"+m.getMotid());
		}
	}

	public void updateUser(Integer deptid) {
		//System.out.println("updateUser11");
		String hql="from UUser where UDept.deptid=?";
		HibernateTemplate h =  this.getHibernateTemplate();
		List<UUser> list = h.find(hql,deptid);
		//System.out.println("updateUser22");
		
		for(UUser u :list){
//			UUser uu = new UUser();
//			uu.setBirthday(u.getBirthday());
//			uu.setIsdelete(u.getIsdelete());
//			uu.setMail(u.getMail());
//			uu.setMobile(u.getMobile());
//			uu.setMotorRooms(u.getMotorRooms());
//			uu.setSex(u.getSex());
//			uu.setTelephone(u.getTelephone());
//			uu.setUDepts(u.getUDepts());
//			uu.setUid(u.getUid());
//			uu.setULogs(u.getULogs());
//			uu.setUname(u.getUname());
//			uu.setUpass(u.getUpass());
//			uu.setUsernote(u.getUsernote());
//			uu.setUserRoles(u.getUserRoles());
//			uu.setUtype(u.getUtype());
//			h.delete(u);
//			h.save(uu);
			u.setUDept(null);
			h.update(u);
		}
		//System.out.println("ddddddddd");
	}

	public List<UUser> list() {
		String hql = "from UUser where utype=1";
		List<UUser> list = this.getHibernateTemplate().find(hql);
		System.out.println(list.size()+"ddddddddddddddd");
		System.out.println("长度："+list.size());
		for(UUser uu:list){
			System.out.println("数据");
			System.out.println(uu.getUname());
		}
		return list;
	}

	public List<UDept> select(UDept d) {
		System.out.println(d.getDeptid()+"节点");
	    StringBuilder sb = new StringBuilder("from UDept where isdelete=0");
	    Integer deptid=d.getDeptid();
	    Integer userid=d.getUUser().getUid();
	    String dname = d.getDeptname();
	    if(d.getDeptid()!=0){
	    	sb.append(" and deptid = "+deptid+"  ");
	    }
	    if(userid!=0){
	    	sb.append(" and UUser.uid = "+userid+" ");
	    }
	    if(dname!=null&&!("".equals(dname))){
	    	sb.append(" and deptname like '%"+dname+"%' ");
	    }
	    String hql = sb.toString();
	    System.out.println(hql);
	    List<UDept> list = this.getHibernateTemplate().find(hql);
	    System.out.println(list.size()+"selectsssssssss");
		return list;
	}

	public List<UDept> list1() {
		String hql = "from UDept where isdelete=0";
		List<UDept> list = this.getHibernateTemplate().find(hql);
		return list;
	}


}
